﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerWatch
{
    class Program
    {
        static Process serProcess = null;
        private static string exeFile = null;
        private static string param = null;
        static void Main(string[] args)
        {
            if (args.Length < 1)
                return;
            try
            {
                Console.Title = "tomcat-ServerWatch";
                var domain= System.AppDomain.CurrentDomain;
                domain.ProcessExit += Domain_ProcessExit;
                exeFile = "\"" + args[1] + "\""; //@"c:\windows\system32\cmd.exe";// "\""+ args[1]+"\"";
                Console.WriteLine("run file :" + exeFile);
                //param = "start ";
                for (int i = 2; i < args.Length; i++)
                {
                    param += "\"" + args[i] + "\" ";
                    Console.WriteLine("args :" + args[i]);
                }
                serProcess = new Process();
                serProcess.StartInfo = new ProcessStartInfo(exeFile, param)
                {
                    RedirectStandardInput = true,
                    UseShellExecute = false
                };

                serProcess.Start();
                serProcess.StandardInput.AutoFlush = true;
                Console.WriteLine(serProcess.Id);
                int restartNum = 0;
                while (1 == 1)
                {
                    if (restartNum > 10)
                        break;
                    if (serProcess.HasExited)
                    {

                        restartNum++;
                        //serProcess = new Process();
                        //serProcess.StartInfo = new ProcessStartInfo(exeFile, param);
                        serProcess.Start();
                    }
                    System.Threading.Thread.Sleep(500);

                }
                string msg = Console.ReadLine();
                while (msg != "q")
                {
                    msg = Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                try
                {
                    if (serProcess != null && !serProcess.HasExited)
                        serProcess.Kill();
                }
                catch
                {
                }
            }
            Console.ReadLine();
        }

        private static void Domain_ProcessExit(object sender, EventArgs e)
        {
            try
            {
                if (serProcess != null && !serProcess.HasExited)
                    serProcess.Kill();
            }
            catch
            {
            }
        }

        private static void SerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}
